package com.epoint.clubmanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.epoint.clubmanagement.common.ResultMapUtil;
import com.epoint.clubmanagement.entity.Club;
import com.epoint.clubmanagement.service.IClubService;
import com.epoint.clubmanagement.service.IClubTypeService;
import com.epoint.clubmanagement.vo.ClubVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Title: ClubController
 * Description: 俱乐部相关的controller
 *
 * @author sqn
 * @version 1.0.0
 * @date 2021/3/17 0017 下午 12:23
 */
@Controller
@RequestMapping(value = "/club")
public class ClubController {

    @Autowired
    private IClubService clubService;

    @Autowired
    private IClubTypeService clubTypeService;

    /**
     * 转向俱乐部类型页面
     */
    @RequestMapping
    @ApiOperation("转向俱乐部类型页面")
    public String club() {
        return "/club";
    }

    /**
     * 分页查询俱乐部列表
     */
    @RequestMapping(value = "/clubQueryPage")
    @ResponseBody
    @ApiOperation("分页查询俱乐部列表")
    public Object clubQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<Club> iPage = clubService.selectClubPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 分页查询俱乐部列表
     */
    @RequestMapping(value = "/clubVOQueryPage")
    @ResponseBody
    @ApiOperation("分页查询俱乐部列表")
    public Object clubVOQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<ClubVO> iPage = clubService.selectClubVOPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向俱乐部类型新增页面
     */
    @RequestMapping(value = "/clubPage")
    @ApiOperation("转向俱乐部类型新增页面")
    public String clubPage() {
        return "/clubPage";
    }

    /**
     * 添加一个俱乐部
     */
    @RequestMapping(value = "/clubAdd")
    @ResponseBody
    @ApiOperation("添加一个俱乐部")
    public Object clubAdd(Club club) {
        try {
            int count = clubService.checkNameRepeatInsert(club);
            if (count > 0) {
                return ResultMapUtil.getHashMapCheckNameRepeat(0);
            }
            int i = clubService.addClub(club);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向俱乐部类型编辑页面
     */
    @RequestMapping(value = "/clubQueryById")
    @ApiOperation("转向俱乐部类型编辑页面")
    public String clubQueryById(@RequestParam(name = "id", required = true) Integer id, Model model) {
        Club club = clubService.queryClubById(id);
        model.addAttribute("obj", club);
        return "/clubEditPage";
    }

    /**
     * 修改一个俱乐部
     */
    @RequestMapping(value = "/clubEdit")
    @ResponseBody
    @ApiOperation("修改一个俱乐部")
    public Object clubEdit(Club club) {
        try {
            int count = clubService.checkNameRepeatUpdate(club);
            if (count > 0) {
                return ResultMapUtil.getHashMapCheckNameRepeat(0);
            }
            int i = clubService.editClub(club);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个俱乐部
     */
    @RequestMapping(value = "/clubDelById")
    @ResponseBody
    @ApiOperation("删除一个俱乐部")
    public Object clubDelById(Integer id) {
        try {
            Club club = clubService.queryClubById(id);
            int numberOfMembers = club.getNumberOfMembers();
            if (numberOfMembers != 0) {
                return ResultMapUtil.getHashMapDel(0);
            }
            int i = clubService.delClubById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有俱乐部
     */
    @RequestMapping(value = "/clubList")
    @ResponseBody
    @ApiOperation("获取所有俱乐部")
    public Object clubList() {
        List<Club> clubList = clubService.queryClubList();
        return ResultMapUtil.getHashMapList(clubList);
    }

}
